package com.formerlyknownasprince.wheresmystuff.models;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import android.util.Log;
/**
 * @author      Tim Swihart 		<email obscured since this file is public>
 * @version     0.2                 
 * @since       2013-03-01
 */
public class User extends ServerObject {
	protected static String baseURL = "/users/";
	
	@JsonProperty("name")
	private String name;
	
	@JsonProperty("email")
	private String email;
	
	@JsonProperty("address")
	private String address;
	
	@JsonProperty("phone_number")
	private String phoneNumber;
	
	//@JsonProperty("items")
	private ArrayList<Item> items;
	
	//public String test = "unset";

	
	public User() {
		this.setName("");
		this.setEmail("");
		this.setAddress("");
		this.setItems(new ArrayList<Item>());
	}
	
	public User(String email) {
		this.setEmail(email);
	}
	
	public User(String email, String name, String address) throws ClientProtocolException, IOException {
		this.setEmail(email);
		this.setName(name);
		this.setAddress(address);
		this.setItems(new ArrayList<Item>());
		try {
			this.create();
		} catch (SyncException se) {
			se.printStackTrace();
		}
	}
	
	public User(ObjectMapper om, JsonNode usr) {
		try {
			User u = om.treeToValue(usr, User.class);
			this.setEmail(u.getEmail());
			this.setName(u.getName());
			this.setAddress(u.getAddress());
			this.setItems(u.getItems());
			this.setObjectMapper(om);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	/**
	 * TODO
	 * @param email
	 * @return
	 */
	public static User find(String email) {
		return new User();
	}

	/**
	 * Adds an item to this user's list
	 * @param i
	 */
	public void addItem(Item i) {
		//this.getItems().add(i);
	}
	
	/**
	 * Removes an Item from this User's list
	 * @param i
	 */
	public void removeItem(Item i) {
		//this.getItems().remove(i);
	}
	
	/**
	 * reports an item to the administrators
	 * @param i
	 * @return
	 */
	public boolean reportItem(Item i) {
		return true;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public void setItems(ArrayList<Item> i) {
		this.items = i;
	}
	
	public ArrayList<Item> getItems() {
		return items;
	}
	
	public int getId(int id) {
		return this.id;
	}
	
	public JsonNode toJson() {
		return om.valueToTree(this);
	}
	
	@Override
	public String getBaseURL() {
		return this.baseURL;
	}
	
}
